<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设计模式解析：简单工厂模式 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-overlay {
            background: linear-gradient(135deg, rgba(41,50,60,0.9) 0%, rgba(72,85,99,0.8) 100%);
        }
        .code-block {
            background-color: #2d3748;
            color: #e2e8f0;
            border-left: 4px solid #4299e1;
        }
        .highlight-box {
            background-color: rgba(66, 153, 225, 0.05);
            border-left: 4px solid #4299e1;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .mermaid {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative h-96 flex items-center justify-center bg-cover bg-center" style="background-image: url('https://images.unsplash.com/photo-1519389950473-47ba0277781c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2070&q=80')">
        <div class="hero-overlay absolute inset-0"></div>
        <div class="container mx-auto px-6 relative z-10 text-center text-white">
            <h1 class="text-4xl md:text-6xl font-bold mb-4 serif">简单工厂模式</h1>
            <p class="text-xl md:text-2xl max-w-3xl mx-auto mb-8">创建对象的设计艺术</p>
            <p class="text-lg md:text-xl max-w-2xl mx-auto opacity-90">通过工厂类创建对象，实现客户端与具体实现的解耦</p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-5xl">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6 card-hover">
                <div class="flex items-center mb-4">
                    <i class="fas fa-lightbulb text-blue-500 text-2xl mr-3"></i>
                    <h2 class="text-2xl font-semibold text-gray-800">核心概念</h2>
                </div>
                <p class="text-gray-700 mb-4">简单工厂模式是一种创建对象的设计模式，它通过一个工厂类来创建对象，而不需要在客户端代码中直接指定对象的具体类。使得客户端不需要了解具体的实现类，从而实现了对象创建过程的解耦。</p>
                <p class="text-gray-700">虽然这种模式简单易用，但当产品种类增加时，工厂类可能变得复杂，从而需要使用更复杂的工厂模式，如工厂方法模式或抽象工厂模式来解决这些问题。</p>
            </div>
        </section>

        <!-- Concept Diagram -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6">
                <div class="flex items-center mb-6">
                    <i class="fas fa-project-diagram text-blue-500 text-2xl mr-3"></i>
                    <h2 class="text-2xl font-semibold text-gray-800">模式架构图</h2>
                </div>
                <div class="mermaid">
                    classDiagram
                        class Client
                        class SimpleFactory {
                            +createProduct(type: String) Product
                        }
                        class Product {
                            <<interface>>
                            +use()
                        }
                        class ProductA {
                            +use()
                        }
                        class ProductB {
                            +use()
                        }
                        
                        Client --> SimpleFactory
                        SimpleFactory --> Product
                        Product <|-- ProductA
                        Product <|-- ProductB
                </div>
            </div>
        </section>

        <!-- Components -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-puzzle-piece text-blue-500 text-2xl mr-3"></i>
                <h2 class="text-2xl font-semibold text-gray-800">主要组成</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md overflow-hidden p-6 card-hover">
                    <h3 class="text-xl font-semibold text-gray-800 mb-3"><i class="fas fa-code text-blue-500 mr-2"></i>产品接口</h3>
                    <p class="text-gray-700">定义了产品的标准接口，规定了所有产品必须实现的方法。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden p-6 card-hover">
                    <h3 class="text-xl font-semibold text-gray-800 mb-3"><i class="fas fa-cubes text-blue-500 mr-2"></i>具体产品</h3>
                    <p class="text-gray-700">实现了产品接口的具体类，每个类代表一种特定的产品类型。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden p-6 card-hover">
                    <h3 class="text-xl font-semibold text-gray-800 mb-3"><i class="fas fa-industry text-blue-500 mr-2"></i>工厂类</h3>
                    <p class="text-gray-700">负责根据客户端的请求决定创建哪个具体产品的实例。</p>
                </div>
            </div>
        </section>

        <!-- Workflow -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-cogs text-blue-500 text-2xl mr-3"></i>
                <h2 class="text-2xl font-semibold text-gray-800">工作机制</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <!-- Step 1 -->
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-blue-100 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                            <span class="text-blue-600 font-bold">1</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">定义产品接口</h3>
                            <p class="text-gray-700 mb-4">首先定义一个产品接口或抽象类，声明产品应该实现的方法。</p>
                            <div class="code-block rounded-lg p-4 mb-4 overflow-x-auto">
                                <pre><code class="text-sm">public interface Product {
    void use();
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Step 2 -->
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-blue-100 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                            <span class="text-blue-600 font-bold">2</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">实现具体产品</h3>
                            <p class="text-gray-700 mb-4">创建多个具体的产品类，这些类实现了产品接口。</p>
                            <div class="code-block rounded-lg p-4 mb-4 overflow-x-auto">
                                <pre><code class="text-sm">public class ProductA implements Product {
    public void use() {
        System.out.println("Using Product A");
    }
}

public class ProductB implements Product {
    public void use() {
        System.out.println("Using Product B");
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Step 3 -->
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-blue-100 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                            <span class="text-blue-600 font-bold">3</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">创建工厂类</h3>
                            <p class="text-gray-700 mb-4">编写工厂类，该类包含一个静态方法，根据客户端传入的参数决定返回哪个具体的产品实例。</p>
                            <div class="code-block rounded-lg p-4 mb-4 overflow-x-auto">
                                <pre><code class="text-sm">public class SimpleFactory {
    public static Product createProduct(String type) {
        if (type.equals("A")) {
            return new ProductA();
        } else if (type.equals("B")) {
            return new ProductB();
        } else {
            throw new IllegalArgumentException("Unknown product type");
        }
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Step 4 -->
                <div class="p-6">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-blue-100 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                            <span class="text-blue-600 font-bold">4</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">客户端代码</h3>
                            <p class="text-gray-700 mb-4">客户端代码通过工厂类请求产品对象，而不需要直接创建具体的产品实例。</p>
                            <div class="code-block rounded-lg p-4 overflow-x-auto">
                                <pre><code class="text-sm">public class Client {
    public static void main(String[] args) {
        Product product = SimpleFactory.createProduct("A");
        product.use();
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Pros & Cons -->
        <section class="mb-16">
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Pros -->
                <div class="bg-green-50 rounded-xl shadow-md overflow-hidden p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-thumbs-up text-green-600 text-2xl mr-3"></i>
                        <h2 class="text-2xl font-semibold text-gray-800">优点</h2>
                    </div>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span class="text-gray-700"><strong>解耦</strong>：客户端代码不需要知道具体的产品类，而只需依赖于工厂类。这使得系统中的客户端与具体的产品实现解耦。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span class="text-gray-700"><strong>集中管理</strong>：工厂类集中管理对象的创建逻辑，使得对象的创建和使用分开，从而使得系统的扩展性和维护性提高。</span>
                        </li>
                    </ul>
                </div>
                <!-- Cons -->
                <div class="bg-red-50 rounded-xl shadow-md overflow-hidden p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-thumbs-down text-red-600 text-2xl mr-3"></i>
                        <h2 class="text-2xl font-semibold text-gray-800">缺点</h2>
                    </div>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-circle text-red-500 mt-1 mr-2"></i>
                            <span class="text-gray-700"><strong>工厂类责任过重</strong>：随着产品类型的增加，工厂类需要不断地添加新的逻辑来创建不同的产品，这可能导致工厂类过于庞大和复杂。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-circle text-red-500 mt-1 mr-2"></i>
                            <span class="text-gray-700"><strong>违反开闭原则</strong>：如果需要添加新的产品类型，必须修改工厂类的代码，这违反了开闭原则（对扩展开放，对修改关闭）。</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Use Cases -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6">
                <div class="flex items-center mb-6">
                    <i class="fas fa-lightbulb text-blue-500 text-2xl mr-3"></i>
                    <h2 class="text-2xl font-semibold text-gray-800">适用场景</h2>
                </div>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="highlight-box p-5 rounded-lg">
                        <h3 class="font-semibold text-lg mb-2"><i class="fas fa-check-circle text-blue-500 mr-2"></i>推荐使用</h3>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>当产品类型较少且不经常变化时</li>
                            <li>客户端只需要知道产品的抽象接口，不关心具体实现</li>
                            <li>需要集中管理和控制对象的创建过程</li>
                        </ul>
                    </div>
                    <div class="bg-red-50 p-5 rounded-lg">
                        <h3 class="font-semibold text-lg mb-2"><i class="fas fa-exclamation-circle text-red-500 mr-2"></i>不推荐使用</h3>
                        <ul class="list-disc pl-5 space-y-2 text-gray-700">
                            <li>产品类型较多且频繁变化</li>
                            <li>需要遵循开闭原则（不修改已有代码即可扩展）的场景</li>
                            <li>产品创建逻辑过于复杂</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="container mx-auto px-6 text-center">
            <div class="mb-4">
                <h3 class="text-xl font-semibold">技术小馆</h3>
            </div>
            <div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors duration-300">
                    <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                </a>
            </div>
            <div class="mt-6 text-gray-400 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true
            }
        });
    </script>
</body>
</html>